import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  server: {
    port: 7777,
    host: '0.0.0.0', // 公网可访问
    allowedHosts: ['www.zehaotarpaulin.cn'],
    fs: {
      allow: [
        fileURLToPath(new URL('./src', import.meta.url)),
        fileURLToPath(new URL('./public', import.meta.url)),
        // 允许访问 node_modules
        'node_modules',
        // 项目根目录
        '/www/wwwroot/zehaopengbu'
      ],
    },

    proxy: {
      '/api': {
        target: 'https://api.zehaotarpaulin.cn', // 目标服务器地址
        changeOrigin: true,
        //rewrite: (path) => path.replace(/^\/api/, ''), // 如果需要路径重写可以打开
      },
    },
  },
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
})
